var BroadCast = {
	currentId: null,
	checkInit: function(){
		BroadCast.broadInit(1);

	},
	broadCastClick: function(pageclickednumber){
		var cont = GetLS("cont");
      	BroadCast.broadInit(pageclickednumber);
      	$("#pager").pager({pagenumber: pageclickednumber, pagecount: cont, buttonClickCallback: BroadCast.broadCastClick });
	},
	broadInit: function(pageno){ 
		 var count = Res.sinCount;
          var start = (pageno-1)*count;
		$ajax({
			url: "/broadCast/list",
			 data: {
                    start : start,
                    count : count
               },
			success: function(res) {
                    var cont = parseInt(res.result.count/Res.sinCount);
                    if((res.result.count)%Res.sinCount!=0){
                         cont++;
                    }
                    SetLS("cont",cont);
                    if($("#pager").children().length == 0)
                     $("#pager").pager({ pagenumber: pageno, pagecount: cont, buttonClickCallback: BroadCast.broadCastClick });

				var html = '<table class="table table-bordered"><thead><tr>'+
                            '<th class="span4">标题</th>'+  
                            '<th class="span5">描述</th>'+
                            '<th class="span3">操作</th>'+
	                    '</tr></thead><tbody>';

	            var items = res.result.results;
	            for (var i=0; i<items.length; i++) {
	            	var btnHtml = '<button class="btn btn-sm updateBtn">修改</button> <button class="btn btn-danger btn-sm removeBtn">删除</button>';
	            	html += '<tr data-pos="'+ i +'">'+
	            				'<td>'+ items[i].title +'</td>'+   
	            				'<td class="umw15">'+ (items[i].content || "") +'</td>'+
	            				'<td>'+ btnHtml +'</td>'+
	            			'</tr>';

	            }
	            html += '</tbody></table>';
	            $("#listCtn").html(html);
	            $(".removeBtn").click(function() {
	            	var item = items[$(this).parents("tr").data("pos")];
		        	if(window.confirm('你确定要删除标题“' +  item.title + '”吗？删除后将不能恢复！')){
		            	$ajax({
		            		url:"/broadCast/remove",
		            		data:{_id:item._id},
		            		success: function() {
		            			window.location.href = "/broadCastManage.html";
		            		}
		            	});
		          	}	            	
	            });
	           	$(".updateBtn").click(function() {
	            	var item = items[$(this).parents("tr").data("pos")];
	            	window.location.href = "/broadCastCreate.html?id="+item._id;
	            });
			}
		});	            
	},

	broadCastInit: function() {
		$("#create").click(BroadCast.add);

		var id = getValue('id');
		if(id) {
			$("#title").text('修改IT方向');
			$("#courseList").show();
			$("#projectList").show();
			$ajax({
				url: "/broadCast/detail",
				data: {
					_id: id,
				},
				success: function(res) {
					var broadCast = res.result;
					$("#title").val(broadCast.title);
					$("#contents").val(broadCast.content);
					
					SetLS("direcdom","projectBottom");
					var length = broadCast.projects && broadCast.projects.length || 0;
					for(var i=0; i<length; i++) 
						BroadCast.addTopDom(broadCast.projects[i].id, broadCast.projects[i].title);

					SetLS("direcdom","courseBottom");
					length = broadCast.courses && broadCast.courses.length || 0;
					for(var i=0; i<length; i++) 
						BroadCast.addTopDom(broadCast.courses[i].id, broadCast.courses[i].title);
				}
			});	
				
		}else { 
			$("#title").text('新建IT方向');
			$("#courseList").hide();
			$("#projectList").hide();
		}
	},
	add: function() {
		var title = $("#title").val();
		var content = $("#contents").val();
		if (title == "" || content == "") 
			return $alert("标题和描述不能为空");

		var id = getValue("id");
		var data = {};
		data.title = title;
		data.content = content;
		if(id)
			data._id = id;
		$ajax({
			url: "/broadCast/upsert",
			data: data,
			success: function(res) {
				window.location.href = "/broadCastManage.html";
			}
		});
	}
}
